package com.example.administrator.myproject.home

import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.os.Handler
import android.support.v4.app.FragmentActivity
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.ViewTreeObserver
import android.view.ViewTreeObserver.OnGlobalLayoutListener
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import cn.bmob.v3.exception.BmobException
import cn.bmob.v3.listener.FindListener
import com.example.administrator.myproject.R
import com.example.administrator.myproject.bean.BobmBean
import com.example.administrator.myproject.bean.CartModel
import java.lang.String

//, private val List: MutableList<CartModel>
class HomeAdapter(private val mContext: FragmentActivity?, private val List: MutableList<CartModel>) : RecyclerView.Adapter<HomeAdapter.Holder>() {
    private var inflater: LayoutInflater
    private var list: List<BobmBean>? = null
    private var mDatas: List<CartModel>? = null
    private var mAmountLeft:Int? = null
    private var mReduceLeft:Int? = null
    private var mAddLeft:Int? = null
    private var mActivity: FragmentActivity? = null
    private var textview2: TextView?= null
    private var tot: Int? = 0

    init {
        this.inflater = LayoutInflater.from(mContext)
        mDatas = List
        mActivity = mContext

    }



    override fun onCreateViewHolder(viewGroup: ViewGroup, i: Int): Holder {
        val view = inflater.inflate(R.layout.cart_item, viewGroup, false)
        return Holder(view)
    }

    override fun onBindViewHolder(holder: Holder, i: Int) {
        holder.money.text = "￥" + list!![i].money
        holder.type.text = list!![i].type
        holder.name.text = list!![i].name
        if (list!![i].name!!.equals("拌饭")) {
            holder.imageView.setImageDrawable(mContext!!.resources.getDrawable(R.drawable.laodiexie))
        } else if (list!![i].name!!.equals("米饭")) {
            holder.imageView.setImageDrawable(mContext!!.resources.getDrawable(R.drawable.refeng))
        } else if (list!![i].name!!.equals("米线")) {
            holder.imageView.setImageDrawable(mContext!!.resources.getDrawable(R.drawable.nanxie))
        } else if (list!![i].name!!.equals("面")) {
            holder.imageView.setImageDrawable(mContext!!.resources.getDrawable(R.drawable.kuzi_9_fen))
        } else {
            holder.imageView.setImageDrawable(mContext!!.resources.getDrawable(R.drawable.mian_fan))
        }
        holder.setItem(i)
//        点击加号
        holder.imageViewAdd.setOnClickListener(View.OnClickListener { v ->
            val cartModel = mDatas!![i]
            //如果该商品数量为0就进行这个动画
            if (cartModel.getCount() === 0) {
                holder.imageViewReduce.setVisibility(View.VISIBLE)
                holder.tvAMount.setVisibility(View.VISIBLE)
                val set = AnimatorSet()
                //减号
                val ta1 = ObjectAnimator.ofFloat(holder.imageViewReduce, "translationX", mAddLeft!! - mReduceLeft!!.toFloat(), 0f)
                val ra1 = ObjectAnimator.ofFloat(holder.imageViewReduce, "rotation", 0f, 360f)
                val aa1 = ObjectAnimator.ofFloat(holder.imageViewReduce, "alpha", 0f, 1f)

                //数字
                val ta2 = ObjectAnimator.ofFloat(holder.tvAMount, "translationX", mAddLeft!! - mAmountLeft!!.toFloat(), 0f)
                val ra2 = ObjectAnimator.ofFloat(holder.tvAMount, "rotation", 0f, 360f)
                val aa2 = ObjectAnimator.ofFloat(holder.tvAMount, "alpha", 0f, 1f)
                set.play(ta1).with(ra1).with(ta2).with(ra2).with(aa1).with(aa2)
                set.setDuration(500).start()
            }
            val set = AnimatorSet()
            set.setDuration(500).start()
            cartModel.setCount(cartModel.getCount() + 1)
            holder.tvAMount.setText(cartModel.getCount().toString())
            textview2 = mContext.findViewById(R.id.pay)
            tot = tot!! + cartModel.getPrice()!!
            textview2!!.setText(tot.toString())
//            val set = AnimatorSet()
//            set.setDuration(500).start()
//            set.addListener(object : Animator.AnimatorListener {
//                override fun onAnimationStart(animation: Animator) {}
//                override fun onAnimationEnd(animation: Animator) {
//                    //移除这个view
//                    //跟新购物车
//                    cartModel.setCount(cartModel.getCount() + 1)
//                    print
//                    holder.tvAMount.setText(cartModel.getCount().toString())
//                }
//
//                override fun onAnimationCancel(animation: Animator) {}
//                override fun onAnimationRepeat(animation: Animator) {}
//            })
            //addGoods2CartAnim((ImageView) v,cartModel);
            //得到加号在屏幕的坐标
        })
        holder.imageViewReduce.setOnClickListener(View.OnClickListener { v ->
            val cartModel = mDatas!![i]
            //如果该商品数量为0就进行这个动画
            if (cartModel.getCount() === 1) {
                val set = AnimatorSet()
                println("!!!!!!!")
                val ta1 = ObjectAnimator.ofFloat(holder.imageViewReduce, "translationX", 0f, mAddLeft!! - mReduceLeft!!.toFloat())
                val ra1 = ObjectAnimator.ofFloat(holder.imageViewReduce, "rotation", 0f, 360f)
                val aa1 = ObjectAnimator.ofFloat(holder.imageViewReduce, "alpha", 1f, 0f)

                val ta2 = ObjectAnimator.ofFloat(holder.tvAMount, "translationX", 0f, mAddLeft!! - mAmountLeft!!.toFloat())
                val ra2 = ObjectAnimator.ofFloat(holder.tvAMount, "rotation", 0f, 360f)
                val aa2 = ObjectAnimator.ofFloat(holder.tvAMount, "alpha", 1f, 0f)
                set.play(ta1).with(ra1).with(ta2).with(ra2).with(aa1).with(aa2)
                set.setDuration(500).start()
            }
            val set = AnimatorSet()
            cartModel.setCount(cartModel.getCount() - 1)
            holder.tvAMount.setText(cartModel.getCount().toString())
            textview2 = mContext.findViewById(R.id.pay)
            tot = tot!! - cartModel.getPrice()!!
            textview2!!.setText(tot.toString())
            if (cartModel.getCount() === 0) {
                Handler().postDelayed({
                    // TODO Auto-generated method stub
                    holder.tvAMount.setVisibility(View.INVISIBLE)
                    holder.imageViewReduce.setVisibility(View.INVISIBLE)
                }, 500)

            }
//            val set = AnimatorSet()
//            set.setDuration(500).start()
//            set.addListener(object : Animator.AnimatorListener {
//                override fun onAnimationStart(animation: Animator) {}
//                override fun onAnimationEnd(animation: Animator) {
//                    //移除这个view
//                    //跟新购物车
//                    cartModel.setCount(cartModel.getCount() + 1)
//                    print
//                    holder.tvAMount.setText(cartModel.getCount().toString())
//                }
//
//                override fun onAnimationCancel(animation: Animator) {}
//                override fun onAnimationRepeat(animation: Animator) {}
//            })
            //addGoods2CartAnim((ImageView) v,cartModel);
            //得到加号在屏幕的坐标
        })


//        holder.itemView.setOnClickListener {
//            val intent = Intent()
//            intent.setClass(mContext, Details::class.java)
//            intent.putExtra("objectId", list!![i].objectId)
//            mContext.startActivity(intent)
//        }
    }

    override fun getItemCount(): Int {
        return if (list == null) 0 else list!!.size
    }

    fun setData(li: List<BobmBean>) {
        this.list = li
        notifyDataSetChanged()
    }


    inner class Holder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val name: TextView
        val type: TextView
        val money: TextView
        val imageView: ImageView
        val imageViewAdd: ImageView
        val imageViewReduce: ImageView
        val tvAMount: TextView

        init {
            imageView = itemView.findViewById(R.id.cart_iv)
            name = itemView.findViewById(R.id.cart_tv1)
            type = itemView.findViewById(R.id.cart_tv2)
            money = itemView.findViewById(R.id.cart_tv3)
            tvAMount = itemView.findViewById(R.id.cart_tv4)
            imageViewAdd = itemView.findViewById(R.id.jia)
            imageViewReduce = itemView.findViewById(R.id.jian)
            imageViewAdd.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
                override fun onGlobalLayout() {
                    //得到加号的左边位置
                    mAddLeft = imageViewAdd.getLeft()
                    imageViewAdd.getViewTreeObserver().removeOnGlobalLayoutListener(this)
                }
            })
            imageViewReduce.viewTreeObserver.addOnGlobalLayoutListener(object : OnGlobalLayoutListener {
                override fun onGlobalLayout() {
                    //得到减号的左边位置
                    mReduceLeft = imageViewReduce.left
                    imageViewReduce.viewTreeObserver.removeOnGlobalLayoutListener(this)
                }
            })
            tvAMount.getViewTreeObserver().addOnGlobalLayoutListener(object : OnGlobalLayoutListener {
                override fun onGlobalLayout() {
                    //得到价格的左边位置
                    mAmountLeft = tvAMount.getLeft()
                    tvAMount.getViewTreeObserver().removeOnGlobalLayoutListener(this)
                }
            })

        }
        fun setItem(position: Int) {
            val cartModel = mDatas!![position]
            name.setText(cartModel.getName())
            tvAMount.setText(String.valueOf(cartModel.getCount()))
            if (cartModel.getCount() > 0) { //数目大于0就显示
                imageViewReduce.visibility = View.VISIBLE
                tvAMount.setVisibility(View.VISIBLE)
            } else { //数目小于0就隐藏
                imageViewReduce.visibility = View.INVISIBLE
                tvAMount.setVisibility(View.INVISIBLE)
            }
        }

    }
}
